package cn.com.dragonsoftbravo.utils;

public class ASCIIUtils {

	public static String fromASCII(String ASCIIs) {
		String[] ASCIIss = ASCIIs.split(",");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ASCIIss.length; i++) {
			sb.append((char) ascii2Char(Integer.parseInt(ASCIIss[i])));
		}
		return sb.toString();
	}

	public static String toASCII(String str) {
		return getIntArrayString(string2ASCII(str));
	}

	private static char ascii2Char(int ASCII) {
		return (char) ASCII;
	}

	private static int char2ASCII(char c) {
		return (int) c;
	}

	private static int[] string2ASCII(String s) {
		if (s == null || "".equals(s)) {
			return null;
		}

		char[] chars = s.toCharArray();
		int[] asciiArray = new int[chars.length];

		for (int i = 0; i < chars.length; i++) {
			asciiArray[i] = char2ASCII(chars[i]);
		}
		return asciiArray;
	}

	private static String getIntArrayString(int[] intArray) {
		return getIntArrayString(intArray, ",");
	}

	private static String getIntArrayString(int[] intArray, String delimiter) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < intArray.length; i++) {
			sb.append(intArray[i]).append(delimiter);
		}
		return sb.toString();
	}

}
